<template>
  <div>
    <ConTitle :topTitle="topTitle"></ConTitle>

    <el-card class="box-card">
      <div class="clearfix">
        <span>
          <el-form :inline="true" :model="query" class="demo-form-inline">
            <el-form-item label="群组名称">
              <el-input
                v-model="query.keyword"
                placeholder="群组名称"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">查询</el-button>
              <span> 搜索结果： {{ total }}</span>
            </el-form-item>
          </el-form></span
        >
        <el-button class="addbtn" @click="addbtnfn">新建群组+</el-button>
      </div>
    </el-card>

    <template>
      <el-table :data="grouplist" style="width: 100%">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="groupName" label="群组名称"> </el-table-column>
        <el-table-column prop="nodeNums" label="传感器节点数量">
        </el-table-column>
        <el-table-column prop="onlineNums" label="在线设备数量">
        </el-table-column>
        <el-table-column prop="cTime" label="创建时间"> </el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="edinfun(scope.row.id, scope.row.groupName)"
              >重命名</el-button
            >
            <el-button type="text" size="small" @click="handlefn(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-dialog
      :title="editId ? '修改群组信息' : '添加群组信息'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        :rules="rules"
        :model="ruleFrom"
        ref="ruleFrom"
        class="demo-form-inline"
      >
        <el-form-item prop="groupName">
          <el-input
            v-model="ruleFrom.groupName"
            placeholder="群组名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addfn">确 定</el-button>
      </span>
    </el-dialog>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.currPage"
      :page-sizes="[2, 4, 5, 10]"
      :page-size="query.pageSize"
      layout=" prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
// , $groupEdit, $groupAdd, $groupDel
import { $grouplist, $groupAdd, $groupEdit, $groupDel } from "@/api";

import ConTitle from "@/components/ConTitle.vue";
export default {
  name: "GateWay",
  components: {
    ConTitle,
  },
  data() {
    return {
      topTitle: {
        type: "设备", //非首页，显示上一级
        text: "节点群组",
        msg: "以分组的形式管理节点设备，提供了搜索。添加新的群组、重命名、移动设备等功能",
      },
      grouplist: [],
      total: 0, //数据总条数
      query: {
        currPage: 1, //当前页数,
        pageSize: 5,
        keyword: "",
      },

      dialogVisible: false,
      ruleFrom: { groupName: "" },
      rules: {
        groupName: [
          { required: true, message: "请输入群组名称", trigger: "blur" },
        ],
      },
      editId: "",
    };
  },
  mounted() {
    this.getData();
  },

  methods: {
    handlefn(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          $groupDel({ id: id }).then((res) => {
            if (res.msg == "success") {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addbtnfn() {
      console.log("添加函数触发");
      this.editId = "";
      this.dialogVisible = true;
    },
    addfn() {
      if (this.editId == "") {
        this.addItem();
      } else {
        this.editItem();
      }
    },
    edinfun(id, name) {
      this.dialogVisible = true;
      this.editId = id;
      this.ruleFrom.groupName = name;
    },

    addItem() {
      this.$refs.ruleFrom.validate((valid) => {
        if (valid) {
          $groupAdd({ groupName: this.ruleFrom.groupName }).then((res) => {
            if (res.msg == "success") {
              this.dialogVisible = false;
              this.getData();
              this.ruleFrom.groupName = "";
            }
          });
        } else {
          return false;
        }
      });
    },
    editItem() {
      this.$refs.ruleFrom.validate((valid) => {
        if (valid) {
          $groupEdit({
            groupName: this.ruleFrom.groupName,
            id: this.editId,
          }).then((res) => {
            if (res.msg == "success") {
              this.dialogVisible = false;
              this.getData();
              this.ruleFrom.groupName = "";
            } else {
              return false;
            }
          });
        }
      });
    },
    getData() {
      $grouplist(this.query).then((res) => {
        console.log(res);
        this.grouplist = res.data;
        this.total = res.total;
      });
    },
    handleSizeChange(value) {
      this.query.pageSize = value;
      this.query.currPage = 1;
      this.getData();
      console.log("每页改变条数");
    },
    handleCurrentChange(page) {
      console.log("每页改变条数");
      this.query.currPage = page;
      this.getData();
    },
    onSubmit() {
      this.getData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
}
.el-tabs {
  margin-top: 20px;
}
.el-pagination {
  background: white;
  text-align: center;
  height: 50px;
  line-height: 50px;
  padding-top: 20px;
}
.el-card {
  margin-top: 10px;
}
.box-card {
  position: relative;
  .addbtn {
    color: white;
    background: #66b1ff;
    position: absolute;
    right: 10px;
    top: 20px;
  }
}
</style>